/*   Shotscreens
 *   Copyright (C) 2010, Vadim Zakondyrin (http://crystalnix.com)
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "filesystem.h"
#include "settingswindow.h"
#include <QImage>
#include <QFileDialog>
#include <QMessageBox>
#include <QFileDialog>
#include <QDir>
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>

FileSystem::FileSystem(QObject *parent) :
        QObject(parent)
{
    settings.setPath(QSettings::IniFormat,QSettings::UserScope,"ShotScreensUploader.ini");
}

/*bool FileSystem::GetPluginVersion(int &version, int &subversion)
{


}

bool FileSystem::ShowOptions()
{
    QMessageBox box;
    box.setModal(true);
    box.information(NULL,"Options","No options for this plugin",QMessageBox::Ok,QMessageBox::NoButton);

    return true;
}

bool FileSystem::ShowAbout()
{
    QMessageBox box;
    box.setModal(true);
    box.information(NULL,"ShotScreens Plugin","ShotScreens Plugin for image saving to filesystem",QMessageBox::Ok,QMessageBox::NoButton);


    return true;
}

bool FileSystem::uploadMemory(const QByteArray &data)
{
    QImage image;
    image.loadFromData(data);
    if (image.isNull())
        return false;
    return image.save(QFileDialog::getSaveFileName(0, tr("Save File"),
                                                   "", tr("PNG (*.png);;JPG (*.jpg)")));
}

void FileSystem::setLoginAndPassword(const QString &l, const QString &p)
{
    login = l;
    password = p;
}*/

void FileSystem::GetPluginName(QString &dest_buffer)
{
   dest_buffer.clear();
   dest_buffer.append("FileSystem Plugin");
}

void FileSystem::GetPluginVersion(int &version, int &subversion)
{
    version=1;
    subversion=0;
}

void FileSystem::SetLoginAndPassword(QString login, QString password)
{
    Q_UNUSED(login);
    Q_UNUSED(password);
}

bool FileSystem::UploadStream(QByteArray &streamData)
{
    QImage image;
    image.loadFromData(streamData);
    if (image.isNull())
        return false;
    return image.save(QFileDialog::getSaveFileName(0, tr("Save File"),
                                               settings.value("DefaultDir").toString(), tr("PNG (*.png);;JPG (*.jpg)")));
    //fileNameDialog.setModal(true);
    //fileNameDialog.show();
    //return true;
}

bool FileSystem::UploadFile(QString fileName)
{
    Q_UNUSED(fileName);
    return false;
}


void FileSystem::ShowOptions(QDialog* settingsWindowDraft)
{
    //QString dir = QFileDialog::getExistingDirectory(0, tr("Open Directory"), ".", QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
    QString dir = settings.value("DefaultDir", QDir::homePath()).toString();
    SettingsWindow* p_sw = new SettingsWindow;
    p_sw->setDefaultDir(dir);
    QVBoxLayout* layout = new QVBoxLayout;
    layout->addWidget(p_sw);
    settingsWindowDraft->setLayout(layout);
    settingsWindowDraft->setMinimumSize(p_sw->minSize());

    settingsWindowDraft->show();
}

Q_EXPORT_PLUGIN2(UPLOADER, FileSystem)
